package org.core.tliaswebmanagement.interceptor;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

/**
 * 拦截器与过滤器区别
 * 1. 拦截器是SpringMVC提供的一种机制，用于在请求处理过程中进行预处理和后处理。
 * 2. 执行顺序不同：先执行过滤器，再执行拦截器；拦截器放行后，还需要过滤器放行
 * 3. 实现接口不同：过滤器实现：javax.servlet.Filter；拦截器实现：org.springframework.web.servlet.HandlerInterceptor
 * 4. 拦截范围不同：过滤器拦截所有资源；拦截器拦截spring中的资源
 */

@Slf4j
@Component
public class DemoInterceptor implements HandlerInterceptor {
    //目标资源执行之前执行-返回值为true表示放行，false表示拦截
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        return HandlerInterceptor.super.preHandle(request, response, handler);
    }
    //目标资源执行之后执行
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        HandlerInterceptor.super.postHandle(request, response, handler, modelAndView);
    }
    //渲染视图之后执行
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        HandlerInterceptor.super.afterCompletion(request, response, handler, ex);
    }
}
